package com.company.project.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.io.IOException;
import java.util.List;
import com.company.project.common.utils.DataResult;

import com.company.project.sys.entity.BookEntity;
import com.company.project.sys.service.BookService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author ZYF
 * @date 2021-05-20 10:20:58
 */
@Controller
@RequestMapping("/")
public class BookController {
    @Autowired
    private BookService bookService;


    /**
    * 跳转到页面
    */
    @GetMapping("/index/book")
    public String book() {
        return "book/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("book/add")
    @ResponseBody
    public DataResult add(@RequestBody BookEntity book){
        bookService.save(book);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("book/delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        bookService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("book/update")
    @ResponseBody
    public DataResult update(@RequestBody BookEntity book){
        bookService.updateById(book);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("book/listByPage")
    @ResponseBody
    public DataResult findListByPage(@RequestBody BookEntity book){
        Page page = new Page(book.getPage(), book.getLimit());
        LambdaQueryWrapper<BookEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        //queryWrapper.eq(BookEntity::getId, book.getId());
        IPage<BookEntity> iPage = bookService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

    @ApiOperation(value = "图书下拉框")
    @GetMapping("book/selectByUser")
    @ResponseBody
    public DataResult selectByUser(String bookId,Integer type){
        List<BookEntity> bookEntityList= bookService.selectByUser(bookId);
        if(type >0){
            BookEntity bookEntity= bookService.getById(bookId);
            if(null != bookEntity){
                bookEntityList.add(bookEntity);
            }
        }

        return DataResult.success(bookEntityList );
    }

    @ApiOperation(value = "导出模板")
    @GetMapping(value = "book/export/exportExcel",  produces = { "application/json;charset=UTF-8" })
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        bookService.exportExcel(request,response);
    }
}
